package com.ymatou.app.api.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

/**
 * 获取用户地址信息列表Call FreeMaker Template自动生成代码
 */
public class AddressListCall extends YmatouBaseCall {
	private static String opurl = "api/Address/AddressList";

	public AddressListCall() {
		super(EnvSetup.getSystemUrl("api_app"), opurl, "GET", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept", "text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getCode() {
		String Code = null;
		try {
			Code = new JSONObject(getReturnData()).getString("Code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getBCode() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("BCode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("Msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return
	 */
	public JSONObject getData() {
		JSONObject Data = null;
		try {
			Data = new JSONObject(getReturnData()).getJSONObject("Data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	/**
	 * 获取AddressList
	 * 
	 * @return
	 */
	public JSONArray getAddressList() {
		JSONArray AddressList = null;
		try {
			AddressList = getData().getJSONArray("AddressList");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return AddressList;
	}

	/**
	 * 获取AddressId
	 * 
	 * @return
	 * @return
	 */
	@SuppressWarnings("null")
	public List<Integer> getAddressId() {
		List<Integer> AddressId = new ArrayList<Integer>();
		int temp;
		String temp2;
		for (int i = 0; i < getAddressList().length(); i++) {
			try {
				temp2 = getAddressList().get(i).toString();
				temp = new JSONObject(temp2).getInt("AddressId");
				if (temp != 0) {
					AddressId.add(i, temp);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return AddressId;
	}
}